import storage from "@/util/storage";
import router from "@/router";
import store from "@/store";
import { oauthLogout } from "@/api/logout";
import { oauthToken } from "@/api/login";
import { systemUserMy } from "@/api/user";
import { systemLoginLoginCheckVerifyCode } from "@/api/user-login";
const state = {
  account: storage.local.get("account") || "",
  token: storage.local.get("token") || "",
  permissions: [],
  role: storage.local.get("role") || "",
  id: storage.local.get("id") || "",
  departId: storage.local.get("departId") || "",
  departmentName: storage.local.get("departmentName") || "",
  roles: JSON.parse(storage.local.get("roles")) || [],
};

const getters = {
  isLogin: (state) => {
    return !!state.token;
  },
};

const actions = {
  login({ commit, dispatch, rootState }, data) {
    return new Promise((resolve, reject) => {
      oauthToken(data)
        .then((res) => {
          commit("setUserData", { token: res.access_token, account: data.username });
          // commit("setDePartId", { token: res.departId });
          if (rootState.data.DICT.length === 0) {
            dispatch("data/getDictAll", {}, { root: true });
            dispatch("department/getSystemDepartmentListNoPage", {}, { root: true });
          }
          dispatch("getUserInfo");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  loginCode({ commit, dispatch, rootState }, data) {
    return new Promise((resolve, reject) => {
      systemLoginLoginCheckVerifyCode(data)
        .then((res) => {
          commit("setUserData", { token: res.token, account: data.mobile });
          // commit("setDePartId", { token: res.departId });
          if (rootState.data.DICT.length === 0) {
            dispatch("data/getDictAll", {}, { root: true });
            dispatch("department/getSystemDepartmentListNoPage", {}, { root: true });
          }
          dispatch("getUserInfo");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  logout({ commit }) {
    const token = store.state.user.token;
    commit("removeUserData");
    commit("menu/invalidRoutes", null, { root: true });
    commit("tabbar/clean", null, { root: true });
    commit("data/SET_USER_MENU_INIT", false, { root: true });
    router.push("/login");
    if (token) {
      oauthLogout({ token });
    }
  },
  getPermissions({ state, commit }, data) {
    return new Promise((resolve) => {
      commit("setPermissions", data);
      resolve();
    });
  },
  getUserInfo({ commit, dispatch, rootState }) {
    return systemUserMy().then((res) => {
      commit("setRole", res.role);
      commit("setId", res.id);
      commit("setAccount", res.realName || res.username);
      commit("setDePartId", res.departId);
      commit("setDepartmentName", res.department);
      commit("setRoles", res.roles);
    });
  },
};

const mutations = {
  setRole(state, role) {
    storage.local.set("role", role);
    state.role = role;
  },
  setId(state, id) {
    storage.local.set("id", id);
    state.id = id;
  },
  setDePartId(state, departId) {
    storage.local.set("departId", departId);
    state.departId = departId;
  },
  setAccount(state, account) {
    storage.local.set("account", account);
    state.account = account;
  },
  setDepartmentName(state, departmentName) {
    storage.local.set("departmentName", departmentName);
    state.departmentName = departmentName;
  },
  setUserData(state, { token, account }) {
    storage.local.set("token", token);
    state.token = token;
  },
  removeUserData(state) {
    storage.local.remove("account");
    storage.local.remove("token");
    state.account = "";
    state.token = "";
  },
  setPermissions(state, permissions) {
    state.permissions = permissions;
  },
  setRoles(state, roles) {
    storage.local.set("roles", JSON.stringify(roles));
    state.roles = roles;
  },
};

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations,
};
